﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.IO;

namespace MadearteNovo.Site.MasterPage
{
    public partial class Site : System.Web.UI.MasterPage
    {
        UserControl.MensagensErro msgErro;
        private bool contemNumeros(string texto)
        {
            if (texto.Where(c => char.IsNumber(c)).Count() > 0)
                return true;
            else
                return false;
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnEnviar_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtNome.Text == string.Empty)
                    throw new ApplicationException("Digite o NOME!");
                if (txtEmail.Text == string.Empty)
                    throw new ApplicationException("Digite o EMAIL!");
                if (!Util.Validacoes.ValidaEmail(txtEmail.Text))
                    throw new ApplicationException("Digite um endereço de EMAIL válido!");
                if (contemNumeros(txtNome.Text))
                    throw new ApplicationException("Digite um NOME no válido!");

                string newsletterText = string.Empty;
                newsletterText = File.ReadAllText(HttpContext.Current.Server.MapPath("../Pages/EnvioNewsletter.html"));

                Dictionary<string, string> replaceValues = new Dictionary<string, string>();
                replaceValues.Add("<#Nome#>", txtNome.Text.Trim());

                string assunto = "Teste de envio de newsletter por e-mail no C# .Net";

                string emailRemetente = "exemplo@yahoo.com"; //Email Madearte
                string emailDestinatario = txtEmail.Text.Trim();

                this.SendMail(newsletterText, replaceValues, emailRemetente, emailDestinatario, assunto);
            }
            catch(Exception ex)
            {
                msgErro = (UserControl.MensagensErro)LoadControl("/UserControl/MensagensErro.ascx");
                msgErro.Erro = ex.Message;
                placeholder.Controls.Add(msgErro);
            }
        }

        private void SendMail(string newsletterText, Dictionary<string, string> replaceValues, string emailRemetente, string emailDestinatario, string assunto)
        {
            foreach (string key in replaceValues.Keys)
                newsletterText = newsletterText.Replace(key, replaceValues[key]);

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(emailRemetente);
            mail.To.Add(new MailAddress(emailDestinatario));
            mail.Subject = assunto;
            mail.Body = newsletterText;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;    
  
            SmtpClient smtp = new SmtpClient("smtp.mail.yahoo.com", 465);
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential("seuemail@yahoo.com.br", "suasenha");
            smtp.Host = "smtp.mail.yahoo.com.br";
            smtp.Port = 465; //587
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.EnableSsl = true;

            smtp.Send(mail);
        }
    }
}